package com.itchen.controller;

import com.itchen.pojo.Brand;
import com.itchen.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author げっしょく
 * @date 2021/9/16
 */

@RestController
@RequestMapping("/brands")
public class BrandController {


    @Autowired
    private BrandService brandService;

    @PostMapping
    public R insert(@RequestBody Brand brand) {
        R insert = brandService.insert(brand);
        return insert;
    }

    @DeleteMapping("/{id}")
    public R delByIdAfter(@PathVariable int id) {
        System.out.println(id);
        R r = brandService.delByIdAfter(id);
        return r;
    }

    @PutMapping
    public R update(@RequestBody Brand brand) {
        R update = brandService.update(brand);
        System.out.println(brand);
        return update;
    }

    @GetMapping
    public R selectAll() {
        R r = brandService.selectAll();
        int i = brandService.selectCount();
        r.setCount(i);
        return r;
    }

    @RequestMapping("/delarry")
    public R delarray(@RequestBody int[] ids) {
//        System.out.println(Arrays.toString(ids));
        R delarray = brandService.delarray(ids);
        return delarray;
    }

    @RequestMapping("/selectBypage")
    public R selectByPage(@RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
//        System.out.println("currentPage: " + currentPage + ";   pageSize: " + pageSize);
        int index = (currentPage - 1) * pageSize;
//        System.out.println(index + "----" + pageSize);
        R r = brandService.selectByPage(index, pageSize);
        int i = brandService.selectCount();
        r.setCount(i);
        return r;
    }


    @RequestMapping("/selectCount")
    public int selectCount() {
        return 0;
    }

    @RequestMapping("/selectByPageAndcond")
    public R selectByPageAndcond(int pageSize, int currentPage, @RequestBody Brand brand) {
        if (brand.getBrandName() != null || brand.getBrandName() != "") {
            String brandName = brand.getBrandName();
            brandName = "%" + brandName + "%";
            brand.setBrandName(brandName);
        }
        if (brand.getCompanyName() != null || brand.getCompanyName() != "") {
            String companyName = brand.getCompanyName();
            companyName = "%" + companyName + "%";
            brand.setCompanyName(companyName);
        }
//
//        System.out.println(brand.getBrandName());
//        System.out.println(brand.getCompanyName());
//        System.out.println(currentPage + "..." + pageSize + "..." + brand);

        int index = (currentPage - 1) * pageSize;
        System.out.println(index + "----" + pageSize);

        R r = brandService.selectByPageAndcond(index, pageSize, brand);
        int i = brandService.selectCountAndcond(brand);
        r.setCount(i);
//        System.out.println(r);
        return r;
    }

    @RequestMapping("/selectCountAndcond")
    public int selectCountAndcond(Brand brand) {
        return 0;
    }
}
